//
//  CCVSImage+Utilities.m
//  ClinicalCollections
//
//  Created by Pasirstein, David B on 4/15/13.
//  Copyright (c) 2013 GOPI. All rights reserved.
//

#import "CCVSImage+Utilities.h"

@implementation CCVSImage (Utilities)

+(void)asyncLoadThumbImageIntoView:(CCVSImage *)image intoCCVSExtendedImageView:(CCVSExtendedImageView *)eiv withSpinner:(BOOL)withSpinner
{
    //first...
    eiv.largeImageURL = image.largeImageURL;
    
    //now everything else
    if (image.CIImage) {
        //don't go any further, we already have what we want in memory
        eiv.image = image;
        eiv.largeImageURL = image.largeImageURL;
        return;
    }
    ////////////////////
    //We need to load an image
    //
    UIActivityIndicatorView * spinner;
    if (withSpinner) {
        spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
        spinner.backgroundColor = [UIColor lightGrayColor];
        spinner.hidesWhenStopped = YES;
        CGPoint spinnerOrigin = [CCVSImage centerInView:spinner inView:eiv];
        spinner.frame = CGRectMake(spinnerOrigin.x, spinnerOrigin.y,
                                   spinner.frame.size.width, spinner.frame.size.height);
        [eiv addSubview:spinner];
        [spinner startAnimating];
    }
    dispatch_queue_t downloadQueue = dispatch_queue_create("image downloader", NULL);
    dispatch_async(downloadQueue, ^{
        NSURL * urlToImage = (image.thumbNailURL) ? image.thumbNailURL : image.largeImageURL;
        NSData * imgdata = [NSData dataWithContentsOfURL:urlToImage];
        dispatch_async(dispatch_get_main_queue(), ^{
            CCVSImage* newImg = [[CCVSImage alloc] initWithData:imgdata];
            newImg.largeImageURL = image.largeImageURL;
            newImg.thumbNailURL = image.thumbNailURL;
            [spinner stopAnimating];
            [spinner removeFromSuperview];
            eiv.image = newImg;
        });
    });
    
}

+(void)asyncLoadLargeImageIntoView:(CCVSImage *)image intoCCVSExtendedImageView:(CCVSExtendedImageView *)eiv withSpinner:(BOOL)withSpinner
{
    if (!image.largeImageURL && image.CIImage) {
        //no large image to load but we already have a thumb
        eiv.image = image;
        return;
    }
    ////////////////////
    //We need to load an image
    UIActivityIndicatorView * spinner;
    if (withSpinner) {
        spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
        spinner.backgroundColor = [UIColor lightGrayColor];
        spinner.hidesWhenStopped = YES;
        CGPoint spinnerOrigin = [CCVSImage centerInView:spinner inView:eiv];
        spinner.frame = CGRectMake(spinnerOrigin.x, spinnerOrigin.y,
                                   spinner.frame.size.width, spinner.frame.size.height);
        [eiv addSubview:spinner];
        [spinner startAnimating];
    }
    dispatch_queue_t downloadQueue = dispatch_queue_create("image downloader", NULL);
    dispatch_async(downloadQueue, ^{
        NSURL * urlToImage = (image.largeImageURL) ? image.largeImageURL : image.thumbNailURL;
        NSData * imgdata = [NSData dataWithContentsOfURL:urlToImage];
        dispatch_async(dispatch_get_main_queue(), ^{
            CCVSImage* newImg = [[CCVSImage alloc] initWithData:imgdata];
            newImg.largeImageURL = image.largeImageURL;
            newImg.thumbNailURL = image.thumbNailURL;
            [spinner stopAnimating];
            [spinner removeFromSuperview];
            eiv.image = newImg;
        });
    });
}

+(CGPoint)centerInView:(UIView*)view inView:(UIView*)inView
{
    CGPoint xy = CGPointMake(0, 0);
    CGSize inViewSize = inView.frame.size;
    CGSize viewSize = view.frame.size;

    xy.x = (inViewSize.width / 2) - (viewSize.width / 2);
    xy.y = (inViewSize.height / 2) - (viewSize.height / 2);
    
    return xy;
}




@end
